import java.io.*;
import java.util.ArrayList;
import java.util.Random;

//案例：点名器
//需求：我在一个文件里存储了班级同学的姓名，每一个姓名占一行，要求通过程序实现随机点名器。
public class RandomRoll {
    public static void main(String[] args) throws IOException {
        String path = "MyFile\\src\\tmp\\StuInfo.txt";
        CreateStuInfo(path);
        ArrayList<String> alt = new ArrayList<>();
        BufferedReader br = new BufferedReader(new FileReader(path));
        String line;
        while((line = br.readLine()) != null){
            alt.add(line);
        }
        Random rd = new Random();
        int x = rd.nextInt(alt.size());
        System.out.println("抽中的下标为：" + x);
        System.out.println("抽中的同学为：" + alt.get(x));

    }

    public static void CreateStuInfo(String path) throws IOException {

        BufferedWriter bw = new BufferedWriter(new FileWriter(path));
        bw.write("Zhang San");
        bw.newLine();
        bw.write("Li Si");
        bw.newLine();
        bw.write("Wang Wu");
        bw.newLine();
        bw.write("Zhao Liu");
        bw.newLine();
        bw.write("Qian Qi");
        bw.newLine();
        bw.flush();
        bw.close();
    }
}
